<template>
  <div>
    <Header />
    <Swiper :swiperList="swiperList" />
    <NavItems :iconList="iconList" />
    <Recommend :recommendList="recommendList" />
    <Weekend :weekendList="weekendList" />
  </div>
</template>

<script>
import Header from './components/Header'
import Swiper from './components/Swiper'
import NavItems from './components/NavItems'
import Recommend from './components/Recommend'
import Weekend from './components/Weekend'

import { get } from '../../utils/http'
import { mapState } from 'vuex'

export default {
  name: 'Home',

  components: {
    Header,
    Swiper,
    NavItems,
    Recommend,
    Weekend,
  },

  computed: {
    ...mapState(['city']),
  },

  data() {
    return {
      swiperList: [],
      iconList: [],
      recommendList: [],
      weekendList: [],
      lastCity: '',
    }
  },

  methods: {
    getHomeData() {
      get(`./api/index.json?city=${this.city}`).then(this.getHomeDataSucc)
    },

    getHomeDataSucc(res) {
      res = res.data
      if (res.ret && res.data) {
        this.iconList = res.data.iconList
        this.recommendList = res.data.recommendList
        this.swiperList = res.data.swiperList
        this.weekendList = res.data.weekendList
      }
    },
  },

  mounted() {
    this.getHomeData()
    this.lastCity = this.city
  },

  activated() {
    if (this.lastCity !== this.city) {
      this.lastCity = this.city
      this.getHomeData()
    }
  },
}
</script>

<style lang="scss" scoped></style>
